import { Component, OnInit, ViewChild } from '@angular/core';
import { ModalDirective } from 'ng2-bootstrap/modal';
import { PageModel } from "../../../../model/page-model";
import { flyIn } from '../../../../animations/fly-in';
import { MyHttp } from '../../../../service/MyHttp';
@Component({
  selector: 'app-grouplist',
  templateUrl: './grouplist.component.html',
  styleUrls: ['./grouplist.component.css']
})

export class GrouplistComponent implements OnInit {
  @ViewChild('myModal') public myModal: ModalDirective;
  public disabled:string =  '';
  pageModel = new PageModel();
  page: number = 1;
  public keyWord: string = '';
	private key_word: string = '';
  name: any;
  IDnum: any;
  phone: any;
  btype: number;
  group_name_new: any;
  group_address_new: any;
  group_name_edit: any;
  group_address_edit: any;
  groupdata: any;
  group_id: any;
  // deviceIds: any;
  public device_module_numbers = [{id:''}];
  groupstat: any;
  groupname: string = '';
  policy_id: any;
  policyList: any;
  constructor(private myHttp: MyHttp) { }
  ngOnInit() {
    this.key_word = this.keyWord = localStorage.getItem('group_group_key_word')?localStorage.getItem('group_group_key_word'):'';
    this.getdata(1);
    this.getPolicyList();
  }

  getdata(page) {
    this.page = page;
    this.myHttp.post('group/findAll?page='+page+'&key_word='+this.key_word, {}).subscribe(res => {
      if (res.errorCode == 0) {
        for (let item of res.message.data) {
          item.group_device_num = item.group_devices ? item.group_devices.split(',').length : 0;
        }
        this.groupdata = res.message.data;
      }

    });
  }
  getPolicyList() {
    this.myHttp.post('policy/list', {}).subscribe(res => {
      if (res.errorCode == 0) {
        this.policyList = res.message;
      }
    });
  }
  savedata() {
    this.disabled = 'disabled';
    this.myHttp.post('group/create',
      {
        "group_name": this.group_name_new,
        "group_address": this.group_address_new,
      }).subscribe(res => {
        if (res.errorCode == 0) {
          alert(res.message);
          this.myModal.hide();
          this.group_name_new = '';
          this.group_address_new = '';
          this.getdata(this.page);
        }
        else {
          alert(res.message)
        }
        this.disabled = '';
      }, err => { alert('保存失败');this.disabled = ''; });
  }

  edit(obj) {
    this.myModal.show();
    this.btype = 2;
    this.group_name_edit = obj.group_name;
    this.group_address_edit = obj.group_address;
    this.group_id = obj.id;

  }
  saveedit() {
    this.myHttp.post('group/update',
      {
        "group_id": this.group_id,
        "group_name": this.group_name_edit,
        "group_address": this.group_address_edit,
      }).subscribe(res => {
        if (res.errorCode == 0) {
          alert(res.message);
          this.myModal.hide();
          this.getdata(this.page);
        }
      });
  }

  showdevices(obj) {
    this.myModal.show();
    this.group_id = obj.id;
    this.btype = 5;
    this.device_module_numbers = obj.group_devices?obj.group_devices.split(',').map(element => {
      return {id:element};
    }):[{id:''}];
    // this.deviceIds = obj.group_devices;
  }

  savegroupdevices() {
    let ids = this.device_module_numbers.map(element=>{
      return element.id;
    }).filter(element=>{
      return !!element;
    }).join(',');
    this.myHttp.post('group/updateDevices',
      {
        "group_id": this.group_id,
        "group_devices": ids
      }).subscribe(res => {
        if (res.errorCode == 0) {
          alert(res.message);
          this.myModal.hide();
          this.getdata(this.page);
        } else {
          alert(res.message);
        }
      });
  }

  showstatus(obj) {
    this.myModal.show();
    this.btype = 3;
    this.group_id = obj.id;
    this.groupstat = obj.group_status;
    this.groupname = obj.group_name;
  }
  groupstatus() {
    this.myHttp.post('group/updateStatus',
      {
        "group_id": this.group_id,
        "group_status": 1 - this.groupstat
      }).subscribe(res => {
        if (res.errorCode == 0) {
          alert(res.message);
          this.myModal.hide();
          this.getdata(this.page);
        }
      });
  }


  showpolicy(obj) {
    this.group_id = obj.id;
    this.policy_id = obj.policy_id?obj.policy_id:'';
    this.myModal.show();
    this.btype = 4;
  }

  savepolicy() {
    this.myHttp.post('group/updatePolicy',
      {
        "group_id": this.group_id,
        "policy_id": this.policy_id
      }).subscribe(res => {
        if (res.errorCode == 0) {
          alert(res.message);
          this.myModal.hide();
          this.getdata(this.page);
        }
      });
  }
  search() {
    localStorage.setItem('group_group_key_word',this.keyWord);
    console.log(localStorage.getItem('group_group_key_word'))
		this.key_word = this.keyWord;
		this.getdata(1);
		this.pageModel.currentPage = 1;
	}
  cancel() {
    this.myModal.hide();
    this.name = "";
    this.IDnum = "";
    this.phone = "";
    this.policy_id = "";
  }






}
